<?php
$starttime =  time();

$templatefile = "includes/template.html";
include "includes/vars.php";
include "includes/inc_view.php";
$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die("Cannot connect to database server");

mysql_select_db($dbname)
  or Die("Database not found.");



// content management



if (isset($_GET['act']) && $_GET['act'] == "search" && trim($_POST['keywords']) != ""){

//prepare the sql depending on the choices
        if ($_POST['keywordtype'] == "all"){
            $and_or = "AND";
        }
        else if ($_POST['keywordtype'] == "any"){
            $and_or = "OR";
        }
    
//BOOKS CONTENTS
    if ($_POST['searcharea'] == "books")
	{

        $sql = "SELECT CHAP_ID, CHAP_NAME, CHAP_BK_ID FROM chapters WHERE ";

        if($_POST['keywordtype'] == "sentence"){
            $sql .= " CHAP_CONTENT LIKE '%" . trim($_POST['keywords']) . "%'" ;
        }
        else
		{
            $sql = "SELECT CHAP_ID, CHAP_NAME, CHAP_BK_ID FROM chapters WHERE ";
            
            $keywords = explode(" ", trim($_POST['keywords']));
            $wordcount = count ($keywords);
            $x = 1;
            foreach ($keywords as $singleword)
			{
                if (trim($singleword) != "")
				{
                    $sql .= " CHAP_CONTENT LIKE '%" . $singleword . "%' ";
                    if ($x < $wordcount)
					{
                        $sql .= $and_or;
                    }
                }
                $x += 1;
            }
           $sql .= " ORDER BY CHAP_BK_ID ASC ";
        }

		if ($result = mysql_query($sql))
		{
			$resultscount = mysql_num_rows($result);
			if ($resultscount > 0)
			{
				$searchresults = "Total results found: " . $resultscount . "<BR><BR>";
				if (!isset($_GET['target']))
				{
					$_GET['target'] = 0;
				}
				else
				{
					mysql_data_seek($result, $_GET['target']);
				}

				$startSearchDisplay = $_GET['target'] + 1;

				if (($_GET['target'] + $searchresultcount) < $resultscount)
				{
					$searchresults .= "Results: " . $startSearchDisplay  . " to " . ($_GET['target'] + $searchresultcount) . "<BR><BR>";
				}
				else
				{
					$searchresults .= "Results: " . $startSearchDisplay  . " to " . ($_GET['target'] + ($resultscount - ($_GET['target'] + $searchresultscount))) . "<BR><BR>";
				}

				if ($_GET['target'] < $resultscount)
				{
					$x = 0;
					while (($row = mysql_fetch_row($result)) && $x < $searchresultcount)
					{
						// get the book's name
						$sql2 = "SELECT B_NAME FROM books WHERE B_ID = " . $row[2];
						if ($result2 = mysql_query($sql2))
						{
							$row2 = mysql_fetch_row($result2);
							$bookname = $row2[0];
						}
						else
						{
							$bookname = "";
						}

						// print the chapter name
						$searchresults .= '<FONT face=Verdana size=1><A HREF="view.php?bkID=' . $row[2] . '&chapter=' . $row[0] . '">' . $bookname . " : " .$row[1] . '</A></FONT><BR><BR>';
						$x += 1;
					}
			    
					$searchresults .=  '<table border="0" width="100%" cellpadding="5">
						<tr>
						<td width="100%" align="left">';
						if (($_GET['target'] - $searchresultcount) >= 0)
						{
						//print the pervious
							$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] - $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Previous results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						<td width="100%" align="right">';
						if (($_GET['target'] + $searchresultcount) < $resultscount)
						{
			    
						// print the next page link
						$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] + $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Next results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						</tr>
						</table>';
							    
				}
			}
			else
			{
				$searchresults = "No results were found.";
			}
		}



        
		$content = $searchresults;        
    }
    
//AUTHOR NAMES
    if ($_POST['searcharea'] == "authors")
	{
        $sql = "SELECT * FROM authors WHERE ";

        if($_POST['keywordtype'] == "sentence")
		{
            $sql .= " A_FIRST_NAME LIKE '%" . $_POST['keywords'] . "%' OR A_LAST_NAME LIKE '%" . $_POST['keywords'] . "%'" ;
        }
        else
		{
            $sql = "SELECT * FROM authors WHERE ";

            $keywords = explode(" ", $_POST['keywords']);
            $wordcount = count ($keywords);

            foreach ($keywords as $singleword)
			{
                $x = 2;
                if (trim($singleword) != "")
				{
                    $sql .= " A_FIRST_NAME LIKE '%" . $_POST['keywords'] . "%' OR A_LAST_NAME LIKE '%" . $_POST['keywords'] . "%'" ;
                    $x += 1;
                    if ($x < $wordcount)
					{
                        $sql .= $and_or;
                    }
                }
            }
           $sql .= " ORDER BY A_FIRST_NAME ASC ";
        }

        if ($result = mysql_query($sql))
		{
            $resultscount = mysql_num_rows($result);

            if ($resultscount > 0)
			{
				$searchresults = "Total results found: " . $resultscount . "<BR><BR>";

				if (!isset($_GET['target']))
				{
					$_GET['target'] = 0;
				}
				else
				{
					mysql_data_seek($result, $_GET['target']);
				}

				if (($_GET['target'] + $searchresultcount) < $resultscount)
				{
					$searchresults .= "Results: " . $_GET['target'] . " to " . ($_GET['target'] + $searchresultcount) . "<BR><BR>";
				}
				else
				{
					$searchresults .= "Results: " . $_GET['target'] . " to " . ($_GET['target'] + ($resultscount - ($_GET['target'] + $searchresultscount))) . "<BR><BR>";
				}




            
				if ($_GET['target'] < $resultscount)
				{
					$x = 0;
					while (($row = mysql_fetch_row($result)) && $x < $searchresultcount)
					{
						// print the chapter name
						$searchresults .= '<FONT face=Verdana size=1><A HREF="view.php?authID=' . $row[0] . '">' . $row[1] . ' ' . $row[2] . '</A></FONT><BR><BR>';
						$x += 1;
					}

					$searchresults .=  '<table border="0" width="550" cellpadding="5">
						<tr>
						<td width="100%" align="left">';
						if (($_GET['target'] - $searchresultcount) >= 0)
						{
						//print the pervious
							$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] - $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Previous results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						<td width="100%" align="right">';
						if (($_GET['target'] + $searchresultcount) < $resultscount)
						{
			    
						// print the next page link
						$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] + $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Next results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						</tr>
						</table>';
							    

				}
            }
            else
			{
                $searchresults = "No results were found.";
            }

        }




        
		$content = $searchresults;

    }

	//Chapters NAMES
    if ($_POST['searcharea'] == "chapters")
	{

        $sql = "SELECT CHAP_ID, CHAP_NAME, CHAP_BK_ID FROM chapters WHERE ";

        if($_POST['keywordtype'] == "sentence"){
            $sql .= " CHAP_NAME LIKE '%" . trim($_POST['keywords']) . "%'" ;
        }
        else
		{
            $sql = "SELECT CHAP_ID, CHAP_NAME, CHAP_BK_ID FROM chapters WHERE ";
            
            $keywords = explode(" ", trim($_POST['keywords']));
            $wordcount = count ($keywords);
            $x = 1;
            foreach ($keywords as $singleword)
			{
                if (trim($singleword) != "")
				{
                    $sql .= " CHAP_NAME LIKE '%" . $singleword . "%' ";
                    if ($x < $wordcount)
					{
                        $sql .= $and_or;
                    }
                }
                $x += 1;
            }
           $sql .= " ORDER BY CHAP_BK_ID ASC ";
        }

		if ($result = mysql_query($sql))
		{
			$resultscount = mysql_num_rows($result);
			if ($resultscount > 0)
			{
				$searchresults = "Total results found: " . $resultscount . "<BR><BR>";
				if (!isset($_GET['target']))
				{
					$_GET['target'] = 0;
				}
				else
				{
					mysql_data_seek($result, $_GET['target']);
				}

				$startSearchDisplay = $_GET['target'] + 1;

				if (($_GET['target'] + $searchresultcount) < $resultscount)
				{
					$searchresults .= "Results: " . $startSearchDisplay  . " to " . ($_GET['target'] + $searchresultcount) . "<BR><BR>";
				}
				else
				{
					$searchresults .= "Results: " . $startSearchDisplay  . " to " . ($_GET['target'] + ($resultscount - ($_GET['target'] + $searchresultscount))) . "<BR><BR>";
				}

				if ($_GET['target'] < $resultscount)
				{
					$x = 0;
					while (($row = mysql_fetch_row($result)) && $x < $searchresultcount)
					{
						// get the book's name
						$sql2 = "SELECT B_NAME FROM books WHERE B_ID = " . $row[2];
						if ($result2 = mysql_query($sql2))
						{
							$row2 = mysql_fetch_row($result2);
							$bookname = $row2[0];
						}
						else
						{
							$bookname = "";
						}

						// print the chapter name
						$searchresults .= '<FONT face=Verdana size=1><A HREF="view.php?bkID=' . $row[2] . '&chapter=' . $row[0] . '">' . $bookname . " : " .$row[1] . '</A></FONT><BR><BR>';
						$x += 1;
					}
			    
					$searchresults .=  '<table border="0" width="550" cellpadding="5">
						<tr>
						<td width="100%" align="left">';
						if (($_GET['target'] - $searchresultcount) >= 0)
						{
						//print the pervious
							$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] - $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Previous results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						<td width="100%" align="right">';
						if (($_GET['target'] + $searchresultcount) < $resultscount)
						{
			    
						// print the next page link
						$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] + $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Next results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						</tr>
						</table>';
							    
				}
			}
			else
			{
				$searchresults = "No results were found.";
			}
		}



        
		$content = $searchresults;        
    }
    
//Book NAMES
    if ($_POST['searcharea'] == "titles")
	{
        $sql = "SELECT * FROM books WHERE ";

        if($_POST['keywordtype'] == "sentence")
		{
            $sql .= " B_NAME LIKE '%" . $_POST['keywords'] . "%'";
        }
        else
		{
            $sql = "SELECT * FROM books WHERE ";

            $keywords = explode(" ", $_POST['keywords']);
            $wordcount = count ($keywords);

            foreach ($keywords as $singleword)
			{
                $x = 2;
                if (trim($singleword) != "")
				{
                    $sql .= " B_NAME LIKE '%" . $_POST['keywords'] . "%'";
                    $x += 1;
                    if ($x < $wordcount)
					{
                        $sql .= $and_or;
                    }
                }
            }

           $sql .= " ORDER BY B_NAME ASC ";

        }

        if ($result = mysql_query($sql))
		{
            $resultscount = mysql_num_rows($result);

            if ($resultscount > 0)
			{
				$searchresults = "Total results found: " . $resultscount . "<BR><BR>";

				if (!isset($_GET['target']))
				{
					$_GET['target'] = 0;
				}
				else
				{
					mysql_data_seek($result, $_GET['target']);
				}

				if (($_GET['target'] + $searchresultcount) < $resultscount)
				{
					$searchresults .= "Results: " . $_GET['target'] . " to " . ($_GET['target'] + $searchresultcount) . "<BR><BR>";
				}
				else
				{
					$searchresults .= "Results: " . $_GET['target'] . " to " . ($_GET['target'] + ($resultscount - ($_GET['target'] + $searchresultscount))) . "<BR><BR>";
				}




            
				if ($_GET['target'] < $resultscount)
				{
					$x = 0;
					while (($row = mysql_fetch_row($result)) && $x < $searchresultcount)
					{
						// print the chapter name
						$searchresults .= '<FONT face=Verdana size=1><A HREF="view.php?bkID=' . $row[0] . '">' . $row[1] . '</A></FONT><BR><BR>';
						$x += 1;
					}

					$searchresults .=  '<table border="0" width="550" cellpadding="5">
						<tr>
						<td width="100%" align="left">';
						if (($_GET['target'] - $searchresultcount) >= 0)
						{
						//print the pervious
							$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] - $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Previous results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						<td width="100%" align="right">';
						if (($_GET['target'] + $searchresultcount) < $resultscount)
						{
			    
						// print the next page link
						$searchresults .= '<form method="POST" action="search.php?act=search&target=';
												$searchresults .= $_GET['target'] + $searchresultcount;
												$searchresults .='">
										<input type="submit" value="Next results" name="search">
										<input type="hidden" name="keywords" value="';
												$searchresults .= $_POST['keywords'];
												$searchresults .= '">
										<input type="hidden" name="keywordtype" value="';
										$searchresults .= $_POST['keywordtype'];
										$searchresults .= '">
										<input type="hidden" name="searcharea" value="';
										$searchresults .= $_POST['searcharea'];
										$searchresults .= '">
										</form>';
						}
						$searchresults .=  '</td>
						</tr>
						</table>';
							    


				}
            }
            else
			{
                $searchresults = "No results were found.";
            }

        }




        
		$content = $searchresults;

    }

}




// if no request print results
else {

    $content = '<table border="0" width="550" cellpadding="5">
  <tr>
    <td width="100%"><form method="POST" action="search.php?act=search">
  <p><input type="text" name="keywords" size="20">&nbsp;<input type="submit" 
value="search" name="search"><br>
  <br>
  <input type="radio" value="all" checked name="keywordtype">all
  words&nbsp;&nbsp;&nbsp; <input type="radio" name="keywordtype" value="any">any
  word&nbsp;&nbsp;&nbsp; <input type="radio" name="keywordtype" 
value="sentence">sentence<br>
  <br>
  Search area:&nbsp; <select size="1" name="searcharea">
    <option selected value="books">Books Contents</option>
    <option value="titles">Books Titles</option>
    <option value="chapters">Chaperts Titles</option>
    <option value="authors">Authors</option>
  </select> </p>
</form></td>
  </tr>
</table>';

}












// prepare and send final file

if (file_exists($templatefile)){
    $output = implode ("", file($templatefile));
    $output = str_replace("<!--CONTENT-->", $content, $output);
    print $output;

    
}
else{
    print "ERROR";
}


mysql_close ($link);


$endtime = time();
$totaltime = $endtime - $starttime;
print "<!--" . $totaltime . "-->";

?> 

